package com.future.dbutils;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

public class DatabasePool {
	private static Logger logger = LogManager.getLogger(DatabasePool.class);

	private Properties prop;
	private DruidDataSource dataSource;

	private static DatabasePool DATABASEPOOL_INSTANCE = null;

	private DatabasePool() {
		loadProperties();
		createDatasource();
	}

	public static DatabasePool getInstance() {
		if (DATABASEPOOL_INSTANCE == null) {
			DATABASEPOOL_INSTANCE = new DatabasePool();
		}
		return DATABASEPOOL_INSTANCE;
	}

	private void loadProperties() {
		prop = new Properties();
		InputStream in = this.getClass().getClassLoader()
				.getResourceAsStream("\\dbconfig.properties");
		try {
			logger.debug("加载数据库配置文件");
			prop.load(in);
		} catch (IOException e) {
			logger.error(e);
		}
	}

	private DruidDataSource createDatasource() {
		try {
			dataSource = (DruidDataSource) DruidDataSourceFactory
					.createDataSource(prop);
			logger.debug("创建数据库连接池");
		} catch (Exception e) {
			logger.error(e);
		}
		return dataSource;
	}

	public DruidDataSource getDataSource() {
		return dataSource;
	}

}
